package com.zrwl.poscloud.core.restapi.admin.baseInfo;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.service.TeacherAdminRService;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.TeacherAdminRVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zrwl.poscloud.xo.service.HomeworkBatchService;
import com.zrwl.poscloud.xo.vo.HomeworkBatchVO;
/**
 * 作业管理 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-16
 */
@RestController
@Api(value = "作业管理相关接口", tags = {"作业管理相关接口"})
@RequestMapping("/homeworkBatch")
@Slf4j
public class HomeworkBatchRestApi {
    @Autowired
    private HomeworkBatchService homeworkBatchService;
    @Autowired
    private TeacherAdminRService teacherAdminRService;
    @Autowired
    private AdminService adminService;
    @AuthorityVerify
    @ApiOperation(value = "获取作业管理列表", notes = "获取作业管理列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody HomeworkBatchVO homeworkBatchVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取作业管理列表: {}", homeworkBatchVO);
        TeacherAdminRVO currentTeacher = teacherAdminRService.getCurrentTeacher();
        if(currentTeacher == null){
            throw new RuntimeException("该管理员未绑定教师");
        }
        homeworkBatchVO.setTeacherUid(currentTeacher.getEmployeeUid());
        return ResultUtil.successWithData(homeworkBatchService.getPageList(homeworkBatchVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取作业管理列表", notes = "获取作业管理列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody HomeworkBatchVO homeworkBatchVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取作业管理列表: {}", homeworkBatchVO);
        return ResultUtil.successWithData(homeworkBatchService.getAllList(homeworkBatchVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "新增作业管理")    @ApiOperation(value = "新增作业管理", notes = "新增作业管理", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody HomeworkBatchVO homeworkBatchVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增作业管理: {}", homeworkBatchVO);
        TeacherAdminRVO currentTeacher = teacherAdminRService.getCurrentTeacher();
        if(currentTeacher == null){
            throw new RuntimeException("该管理员未绑定教师");
        }
        if(!currentTeacher.getTaskPublishSubjectTypeAll().contains(String.valueOf(homeworkBatchVO.getSubjectType()))){
            throw new RuntimeException("该教师未拥有本科目的作业发布授权");
        }
        homeworkBatchVO.setTeacherUid(currentTeacher.getEmployeeUid());
        return homeworkBatchService.add(homeworkBatchVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "修改作业管理")    @ApiOperation(value = "修改作业管理", notes = "修改作业管理", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody HomeworkBatchVO homeworkBatchVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改作业管理: {}", homeworkBatchVO);
        return homeworkBatchService.edit(homeworkBatchVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "作业发布")    @ApiOperation(value = "作业发布", notes = "作业发布", response = String.class)
    @PostMapping("/editWorkRelease")
    public String editWorkRelease(@Validated({Update.class}) @RequestBody HomeworkBatchVO homeworkBatchVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("作业发布: {}", homeworkBatchVO);
        homeworkBatchVO.setCompanyUid(adminService.getMe().getCompanyUid());
        return homeworkBatchService.editWorkRelease(homeworkBatchVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "作业撤销")    @ApiOperation(value = "作业撤销", notes = "作业撤销", response = String.class)
    @PostMapping("/editWorkRepeal")
    public String editWorkRepeal(@Validated({Update.class}) @RequestBody HomeworkBatchVO homeworkBatchVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("作业撤销: {}", homeworkBatchVO);
        homeworkBatchVO.setCompanyUid(adminService.getMe().getCompanyUid());
        return homeworkBatchService.editWorkRepeal(homeworkBatchVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "删除作业管理")    @ApiOperation(value = "删除作业管理", notes = "删除作业管理", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody HomeworkBatchVO homeworkBatchVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除作业管理: {}", homeworkBatchVO);
        return homeworkBatchService.delete(homeworkBatchVO);
    }

}
